﻿using ERP.EntityFrameworkCore;
using ERP.ERPSystem.RBAC;
using ERP.ERPSystem.RBAC.Dto;
using ERP.Team.RBAC;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ERP.Web.Controllers.RBAC
{
    /// <summary>
    /// 人资系统-xzc
    /// </summary>
    public class UserController : Controller
    {
        private readonly IUserService _userservice;

        public UserController(IUserService userservice)
        {
            _userservice = userservice;
        }
        /// <summary>
        /// 登录视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AllLogin()
        {
            return View();
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Login(string userName, string password)
        {
            var result = await _userservice.LoginAsync(userName, password);
            return Json(result);
        }

        /// <summary>
        /// 新增页面视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UserPage()
        {
            return View();
        }
        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="userdto"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<int> AddUser(UserDto userdto)
        {
            var i = await _userservice.AddUser(userdto);
            return i;
        }
        /// <summary>
        /// 查询用户视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UserQuery()
        {
            return View();
        }
        /// <summary>
        /// 查询用户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> ShowUser()
        {
            try
            {
                var users = await _userservice.ShowUser();
                return Json(new { 
                    code = 0,  // layui规范的成功状态码
                    msg = "",  // 状态信息
                    count = users.Count,  // 数据总数
                    data = users  // 数据列表
                });
            }
            catch (Exception ex)
            {
                return Json(new { 
                    code = 1,  // 错误状态码
                    msg = ex.Message,  // 错误信息
                    count = 0, 
                    data = new object[] { } 
                });
            }
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<JsonResult> DeleteUser(int id)
        {
            try
            {
                var result = await _userservice.DeleteUser(id);
                if (result)
                {
                    return Json(new { success = true, message = "删除成功" });
                }
                else
                {
                    return Json(new { success = false, message = "删除失败：用户不存在或已被删除" });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "删除失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="userdto"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> UpdateUser([FromBody] UserDto userdto)
        {
            try
            {
                var i = await _userservice.UpdateUser(userdto);
                return Json(i);
            }
            catch (Exception ex)
            {
                return Json(0);
            }
        }


        /// <summary>
        /// 用户分配角色
        /// </summary>
        /// <param name="iputUserRoleDto"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<int> AddUserRole(IputUserRoleDto iputUserRoleDto)
        {
            var i = await _userservice.AddUserRole(iputUserRoleDto);
            return i;
        }
        /// <summary>
        /// 获取角色编号
        /// </summary>
        /// <param name="uid">选择的用户</param>
        /// <returns>返回角色编号集合</returns>
        [HttpGet]
        public async Task<List<int>> GetUserRole(int uid)
        {
            var i = await _userservice.GetUserRole(uid);
            return i;
        }
    }
}
